package www.openteam.com.ui.prdctnMngmnt;


import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.throrinstudio.android.common.libs.validator.Form;
import com.throrinstudio.android.common.libs.validator.Validate;

import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.adapter.PrdctnMngmnt.SendRcvAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.SendRcvBean;
import www.openteam.com.contract.PrdctnMngmnt.SendRcv;
import www.openteam.com.presenter.prdctnMngmnt.SendRcvPresenter;
import www.openteam.com.rewrite.MNotEmptyValidator;
import www.openteam.com.rewrite.MTextView;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.ui.business.ScanActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class SendRcvFragment extends mBaseFragment<SendRcvPresenter>
        implements View.OnClickListener, SendRcv.MView, SearchView.OnQueryTextListener {


    @Bind(R.id.SendRcv_actv)
    SearchView SendRcvActv;
    @Bind(R.id.SendRcv_spinner)
    AppCompatSpinner SendRcvSpinner;
    @Bind(R.id.SendRcv_rnso)
    TextView SendRcvRnso;
    @Bind(R.id.SendRcv_note)
    TextView SendRcvNote;
    @Bind(R.id.SendRcv_Rcycleview)
    RecyclerView sendRcvRcycleview;

    public SendIsChecked sendIsChecked;
    LinearLayout SendRcvDialog;
    @Bind(R.id.SendRcv_scan)
    AppCompatImageView SendRcvScan;
    private String TAG = getClass().getSimpleName();
    private SendRcvAdapter sendRcvAdapter;
    private TextView reciverDate;
    private int RESULT = 1;

    @Override
    public boolean onQueryTextSubmit(String query) {
        String stype = mySetting.getString("rCode", "");
        Log.e(TAG, "onQueryTextSubmit: stype" + stype + "--rdson" + query);
        mPresenter.getDataSuccess("01", query);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }


    interface SendIsChecked {
        void ischecked(boolean ischecked);
    }

    public SendRcvFragment() {
        // Required empty public constructor
    }

    public void setSendIsChecked(SendIsChecked sendIsChecked) {
        this.sendIsChecked = sendIsChecked;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_send_rcv, container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initData();
        initView();
        initListener();
        return view;
    }

    @Override
    protected void initData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    @Override
    protected void initListener() {
//        rnso.setOnClickListener(this);
        SendRcvActv.setOnQueryTextListener(this);
        SendRcvScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ScanActivity.class);
                startActivityForResult(intent, RESULT);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        String work_order_id = data.getStringExtra("work_order_id");
        if (requestCode == RESULT && !work_order_id.equals("")) {
//            mProgressDialog.show();
            SendRcvActv.setQuery(work_order_id,true);
//            mPresenter.queryPrud(getRdsno(), work_order_id,  getStartDate(), getEndDate(),
//                    getStype(),
////                    getMoreCom(),getUserId(),getJMark(),
//                    0);
        }

    }

    @Override
    protected void initView() {
        sendRcvRcycleview.setLayoutManager(new LinearLayoutManager(getContext()));
        SendRcvActv.setIconifiedByDefault(true);
        SendRcvActv.setSubmitButtonEnabled(true);
        SendRcvActv.onActionViewExpanded();
//        SendRcvActv.setQueryHint(getString(R.string.bianhao));
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected SendRcvPresenter loadPresenter() {
        return new SendRcvPresenter();
    }


    /**
     * 配置对话框
     *
     * @param dataBean
     */
    private void setdialog(SendRcvBean.DataBeanX.DataBean dataBean) {
        View dialog = getActivity().getLayoutInflater().inflate(R.layout.item_sendrcv_dialog,
                null);
        MTextView ClientRson = (MTextView) dialog.findViewById(R.id.SendRcv_dialog_dialog_client_rson);//工程单号
        MTextView ClientName = (MTextView) dialog.findViewById(R.id.SendRcv_dialog_client_name);//产品编号
        MTextView ProName = (MTextView) dialog.findViewById(R.id.SendRcv_dialog_pro_name);//产品名称
        TextView Part = (TextView) dialog.findViewById(R.id.SendRcv_dialog_part);//部件
        TextView Unit = (TextView) dialog.findViewById(R.id.SendRcv_dialog_unit);//配件
        TextView Procedurename = (TextView) dialog.findViewById(R.id.SendRcv_dialog_procedurename);//工序名称
        TextView Procedure = (TextView) dialog.findViewById(R.id.SendRcv_dialog_procedure);//工序
        TextView Sendoutnum = (TextView) dialog.findViewById(R.id.SendRcv_dialog_sendoutnum);//发外数量
        TextView Proreturenum = (TextView) dialog.findViewById(R.id.SendRcv_dialog_proreturenum);//加工返回数量
        TextView Recivernum = (TextView) dialog.findViewById(R.id.SendRcv_dialog_recivernum);//加工接受数量
        TextView InspectReturnGoodsNum = (TextView) dialog.findViewById(R.id.SendRcv_dialog_inspect_return_goods_num);//品检退货数量
        TextView Note = (TextView) dialog.findViewById(R.id.SendRcv_dialog_note);//备注
        reciverDate = (TextView) dialog.findViewById(R.id.SendRcv_reciverdate);//收货日期
        final CheckBox ischecked = (CheckBox) dialog.findViewById(R.id.SendRcv_ischecked);//是否品检
//        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//        AlertDialog alertDialog = builder.setView(dialog).create();
        final Form form = new Form();
        addNotEmptyWidget(form, Sendoutnum);
        addNotEmptyWidget(form, reciverDate);
        reciverDate.setOnClickListener(this);
        if (dataBean != null) {
            //修改
            ClientRson.setText(dataBean.getProcno());
            ClientName.setText(dataBean.getProdcode());
            ProName.setText(dataBean.getName());
            Part.setText(dataBean.getGroupname());
            Unit.setText(dataBean.getPartID());
            Procedurename.setText(dataBean.getAname());
            Procedure.setText(dataBean.getAprocedure() + "");
            Sendoutnum.setText(dataBean.getOnumb());
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setPositiveButton(getResources().getString(R.string.queding), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (sendIsChecked != null) {
                    sendIsChecked.ischecked(ischecked.isChecked());
                }
                //mPressenter
            }
        });
        builder.setNegativeButton(getResources().getString(R.string.quxiao), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setView(dialog);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
//        builder.show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.SendRcv_rnso:
//                setdialog();
                break;
            case R.id.SendRcv_reciverdate:
                selectCalendar(reciverDate);
                break;
        }
    }

    private void addNotEmptyWidget(Form form, TextView view) {
        Validate notEmpty = new Validate(view);
        notEmpty.addValidator(new MNotEmptyValidator(getContext(), R.string.notempty));
        form.addValidates(notEmpty);
    }

    public void selectCalendar(final TextView v) {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                v.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @Override
    public void onRefresh() {

    }

    public void createProgressDialog(String content) {

        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getContext());
        }
        mProgressDialog.setMessage(content);
        mProgressDialog.show();


    }

    @Override
    public void success(SendRcvBean sendRcvBean) {
        if (null==sendRcvBean.getData()){
            ToastUtil.showToast(getContext(),sendRcvBean.getInfo());
        }
        final List<SendRcvBean.DataBeanX.DataBean> dataBeanList = sendRcvBean.getData().getData();
        sendRcvAdapter = new SendRcvAdapter(getContext(), dataBeanList);
        sendRcvRcycleview.setAdapter(sendRcvAdapter);
        sendRcvAdapter.setOnLongClickListener(new SendRcvAdapter.OnLongClickListener() {
            @Override
            public void longClick(int postion) {
                setdialog(dataBeanList.get(postion));
            }
        });
    }

    @Override
    public void fail(Throwable e) {
        Log.e(TAG, "fail: " + e.getMessage());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
